﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using EnvDTE80;
using Microsoft.Win32;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using EnvDTE;
using GUAPAddin;

namespace AutoUnitTest.Package
{
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
    [ProvideMenuResource("Menus.ctmenu", 1)]
    [ProvideToolWindow(typeof(MyToolWindow))]
    [Guid(GuidList.PackagePkgString)]
    public sealed class AutPackage : Microsoft.VisualStudio.Shell.Package
    {

        internal static DTE2 DteRef;

        private DTE2 InitDteRef()
        {
            return DteRef ?? (DteRef=GetService(typeof(DTE)) as DTE2);
        }

        private void ShowToolWindow(object sender, EventArgs e)
        {
            // Get the instance number 0 of this tool window. This window is single instance so this instance
            // is actually the only one.
            // The last flag is set to true so that if the tool window does not exists it will be created.
            var window = FindToolWindow(typeof(MyToolWindow), 0, true) as MyToolWindow;
            if ((null == window) || (null == window.Frame))
            {
                throw new NotSupportedException(Resources.CanNotCreateWindow);
            }
            var windowFrame = (IVsWindowFrame)window.Frame;
            ErrorHandler.ThrowOnFailure(windowFrame.Show());
        }

        protected override void Initialize()
        {
           // Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
            base.Initialize();

            InitDteRef();

            // Add our command handlers for menu (commands must exist in the .vsct file)
            var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
            if (null != mcs)
            {
                // Create the command for the menu item.
                CommandID menuCommandID = new CommandID(GuidList.PackageCmdSet, 
                    (int)PkgCmdIDList.cmdIdCommand1);
                MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID);
                mcs.AddCommand(menuItem);

                // Create the command for the 2 menu item.
                CommandID menuCommand2ID = new CommandID(GuidList.PackageCmdSet,
                    (int)PkgCmdIDList.cmdIdCommand2);
                MenuCommand menuItem2 = new MenuCommand(MenuItem2Callback, menuCommand2ID);
                mcs.AddCommand(menuItem2);

                // Create the command for the tool window
                CommandID toolwndCommandID = new CommandID(GuidList.PackageCmdSet, 
                    (int)PkgCmdIDList.cmdIdShowDependency);
                MenuCommand menuToolWin = new MenuCommand(ShowToolWindow, toolwndCommandID);
                mcs.AddCommand(menuToolWin);
            }
        }

        private void MenuItemCallback(object sender, EventArgs e)
        {
            EnvDTE.TextSelection selection;
            selection = (EnvDTE.TextSelection) InitDteRef().ActiveDocument.Selection;
            selection.Text = "inserted text";
        }

        private void MenuItem2Callback(object sender, EventArgs e)
        {

        }
    }
}
